//
//  InvitationRevenueViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2019/3/4.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "InvitationRevenueViewController.h"

#import "InviteListModel.h"
#import "UserManager.h"

#import "InviteListCell.h"
#import "AMYestodayListCell.h"
#import "InviteListNewCell.h"

@interface InvitationRevenueViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) InviteListModel *model;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *page;

@end

@implementation InvitationRevenueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = @"1";
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = UIColorFromRGB(0xF7F7F7);
}

#pragma mark - OutPut

- (void)getInvitationRevenueListWithPage:(NSString *)page
{
    @weakify(self);
    if (self.showType == 0) {
        [UserManager getInvitedIncomeWithUserId:[CBTLocalUserManager sharedInstance].userId page:self.page block:^(id data, HttpRequestResult result, NSString *msg) {
            [self_weak_ stopLoading];
            if (result == kHttpRequestResultNormal) {
                InvitationRevenueModel *model = data;
                if ([self_weak_.page intValue] == 1) {
                    self_weak_.dataArray = [NSMutableArray array];
                }
                
                if (model.invitedList.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                    self_weak_.tableView.mj_footer.hidden = YES;
                }else{
                    self_weak_.tableView.mj_footer.hidden = NO;
                    [self_weak_.dataArray addObjectsFromArray:model.invitedList];
                    self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
                }
                if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                    [self_weak_.tableView.mj_header endRefreshing];
                    [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self_weak_.tableView.mj_header endRefreshing];
                    [self_weak_.tableView.mj_footer endRefreshing];
                }
            }else{
                [HFStatusView showFailureMessage:msg];
                if (self_weak_.dataArray.count == 0) {
                    self_weak_.tableView.mj_footer.hidden = YES;
                }
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self_weak_.tableView reloadData];
            });
        }];
    }else if(self.showType == 1){
        [UserManager  getInvestDetailsWithUserId:self_weak_.userId page:self.page block:^(id data, HttpRequestResult result, NSString *msg) {
            [self_weak_ stopLoading];
            if (result == kHttpRequestResultNormal) {
                InvestDetailsModel *model = data;
                if ([self_weak_.page intValue] == 1) {
                    self_weak_.dataArray = [NSMutableArray array];
                }
                
                if (model.investList.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                    self_weak_.tableView.mj_footer.hidden = YES;
                }else{
                    self_weak_.tableView.mj_footer.hidden = NO;
                    [self_weak_.dataArray addObjectsFromArray:model.investList];
                    self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
                }
                if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                    [self_weak_.tableView.mj_header endRefreshing];
                    [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self_weak_.tableView.mj_header endRefreshing];
                    [self_weak_.tableView.mj_footer endRefreshing];
                }
            }else{
                [HFStatusView showFailureMessage:msg];
                if (self_weak_.dataArray.count == 0) {
                    self_weak_.tableView.mj_footer.hidden = YES;
                }
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self_weak_.tableView reloadData];
            });
        }];
    }else{
        [UserManager getInvitedDetailsWithUserId:self_weak_.userId page:self.page block:^(id data, HttpRequestResult result, NSString *msg) {
            [self_weak_ stopLoading];
            if (result == kHttpRequestResultNormal) {
                InvitedDetailsModel *model = data;
                if ([self_weak_.page intValue] == 1) {
                    self_weak_.dataArray = [NSMutableArray array];
                }
                
                if (model.invitedList.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                    self_weak_.tableView.mj_footer.hidden = YES;
                }else{
                    self_weak_.tableView.mj_footer.hidden = NO;
                    [self_weak_.dataArray addObjectsFromArray:model.invitedList];
                    self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
                }
                if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                    [self_weak_.tableView.mj_header endRefreshing];
                    [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self_weak_.tableView.mj_header endRefreshing];
                    [self_weak_.tableView.mj_footer endRefreshing];
                }
            }else{
                [HFStatusView showFailureMessage:msg];
                if (self_weak_.dataArray.count == 0) {
                    self_weak_.tableView.mj_footer.hidden = YES;
                }
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self_weak_.tableView reloadData];
            });
        }];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    if (self.showType == 1) {
        AMYestodayListCell *cell = [AMYestodayListCell createTableView:tableView];
        cell.backgroundColor = UIColorFromRGB(0xf7f7f7);
        InvestDetailsItemModel *model = self.dataArray[indexPath.row];
        cell.coinNameLabel.text = @"PC";
        cell.dateLabel.text     = @"";
//        cell.dateLabel.text = [NSString getShowDateMonthDayWithTime:[model.date longLongValue]];
        cell.amountLabel.text = model.investAmount;
       
        return cell;
    }
    else {
        InviteListNewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.cellType = 1;
        if (self.showType == 2) {
            cell.investmentLabel.text = kLocalizedString(@"投资详情",nil);
        }
        if (self.showType == 0) {
            cell.hiddenSelectIcon = YES;
            cell.investmentLabel.text   = kLocalizedString(@"投资详情",nil);
            cell.invitationRevenueModel = self.dataArray[indexPath.row];
        }else{
            cell.hiddenSelectIcon = NO;
            cell.invitedDetailsModel = self.dataArray[indexPath.row];
        }
        
        [cell setInviteListCellBlock:^(NSInteger type, NSString * _Nonnull investFlag, NSString * _Nonnull userId) {
//            if (self_weak_.showType == 0 /*|| [investFlag intValue] == 0*/) {return;}
            InvitationRevenueViewController *vc = [[InvitationRevenueViewController alloc]init];
            vc.showType = type == 0 ? 1 : 2;
            vc.userId = userId;
            
            vc.navigationItem.title = kLocalizedString(@"好友邀请列表", nil);
            if (type == 0) {
                vc.navigationItem.title = kLocalizedString(@"好友投资详情", nil);
            }
            [self_weak_.navigationController pushViewController:vc animated:YES];
        }];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.showType == 1 ? 60 : 79;
}

#pragma mark - Lazy

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        
        @weakify(self);
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, ScreenWideSide  - (HFIphoneX ? HFIPhoneXSafeAreaPotraitBottom : 0) - (self.showType == 0 ? 68 : 0) - HFNavigationHeight) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = UIColorFromRGB(0xF7F7F7);
        _tableView.showsVerticalScrollIndicator = NO;
        
        _tableView.contentInset   = UIEdgeInsetsMake(6, 0, 6, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.showNoDataEmptyView = YES;
        
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        
        [_tableView setReloadBtnAction:^{
            [self_weak_ startLoading];
            self_weak_.page = @"1";
            [self_weak_ getInvitationRevenueListWithPage:self_weak_.page];
        }];
        
        if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        _tableView.estimatedRowHeight           = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerNib:[UINib nibWithNibName:@"InviteListNewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
        if (self.showType == 1) {
            [self.tableView registerClass:[AMYestodayListCell class] forCellReuseIdentifier:@"CELL"];
        }
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self_weak_.page = @"1";
            [self_weak_ getInvitationRevenueListWithPage:self_weak_.page];
        }];
        
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self_weak_ getInvitationRevenueListWithPage:self_weak_.page];
        }];
        
        _tableView.tableFooterView  = [UIView new];
        _tableView.mj_footer.hidden = YES;
        [_tableView.mj_header beginRefreshing];
    }
    return _tableView;
}


@end
